package servlets;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class URLgetter
 */
public class URLgetter extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public URLgetter() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String xsl = "";
		byte[] buffer = new byte[1000];
		URL u = new URL(request.getParameter("xsl"));
		URLConnection con = u.openConnection();
	    con.setConnectTimeout(2000);
	    con.setReadTimeout(1000);
		InputStream is = con.getInputStream();
	    DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
	    while ((dis.read(buffer)>0) ) {
            xsl+=new String(buffer);
         }
	    xsl = xsl.substring(0, xsl.indexOf("</xsl:stylesheet>")+"</xsl:stylesheet>".length());
	    response.getOutputStream().write(xsl.getBytes());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
